package com.guiji.job.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.guiji.common.core.domain.entity.SysDictData;
import com.guiji.common.core.mybatisplus.core.ServicePlusImpl;
import com.guiji.common.core.page.PagePlus;
import com.guiji.common.core.page.TableDataInfo;
import com.guiji.common.utils.PageUtils;
import com.guiji.job.bo.JobMarkRecordAddBo;
import com.guiji.job.bo.JobMarkRecordEditBo;
import com.guiji.job.bo.JobMarkRecordQueryBo;
import com.guiji.job.domain.JobMarkRecord;
import com.guiji.job.mapper.JobMarkRecordMapper;
import com.guiji.job.service.IJobMarkRecordService;
import com.guiji.job.vo.JobLabelVo;
import com.guiji.job.vo.JobMarkRecordVo;
import com.guiji.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 任务打标记录Service业务层处理
 *
 * @author 钟维澎（四月）
 * @date 2021-07-02
 */
@Service
public class JobMarkRecordServiceImpl extends ServicePlusImpl<JobMarkRecordMapper, JobMarkRecord> implements IJobMarkRecordService {

	@Autowired(required=false)
	private JobMarkRecordMapper markRecordMapper;

	@Autowired
	private ISysDictTypeService dictTypeService;

	@Override
	public JobLabelVo queryJobLabelVo(Long jobId) {

		JSONObject jsonObject = new JSONObject();
		JobLabelVo vo = markRecordMapper.selectOneRecord(jobId);
		if (ObjectUtil.isNotNull(vo) && ObjectUtil.isEmpty(vo.getBackup1())) {
			if (ObjectUtil.isNotEmpty(vo.getDictType())) {
				List<SysDictData> sysDictDataList = dictTypeService.selectDictDataByType(vo.getDictType());
				sysDictDataList.forEach(e->{ jsonObject.putOpt(e.getDictValue(),""); });
				vo.setBackup1(jsonObject.toString());
			}else{
				jsonObject.putOpt("当前资源没有配置模板","");
				vo.setBackup1(jsonObject.toString());
			}
		}

		return vo;
	}

	@Override
	public JobMarkRecordVo queryById(Long id) {
		return getVoById(id, JobMarkRecordVo.class);
	}

	@Override
	public TableDataInfo<JobMarkRecordVo> queryPageList(JobMarkRecordQueryBo bo) {
		PagePlus<JobMarkRecord, JobMarkRecordVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), JobMarkRecordVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<JobMarkRecordVo> queryList(JobMarkRecordQueryBo bo) {
		return listVo(buildQueryWrapper(bo), JobMarkRecordVo.class);
	}

	private LambdaQueryWrapper<JobMarkRecord> buildQueryWrapper(JobMarkRecordQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<JobMarkRecord> lqw = Wrappers.lambdaQuery();
		lqw.eq(bo.getId() != null, JobMarkRecord::getId, bo.getId());
		lqw.eq(bo.getJobId() != null, JobMarkRecord::getJobId, bo.getJobId());
		lqw.eq(StrUtil.isNotBlank(bo.getKey()), JobMarkRecord::getKey, bo.getKey());
		lqw.eq(StrUtil.isNotBlank(bo.getValue()), JobMarkRecord::getValue, bo.getValue());
		lqw.eq(bo.getVersion() != null, JobMarkRecord::getVersion, bo.getVersion());
		lqw.eq(bo.getCreateTime() != null, JobMarkRecord::getCreateTime, bo.getCreateTime());
		lqw.eq(StrUtil.isNotBlank(bo.getCreateBy()), JobMarkRecord::getCreateBy, bo.getCreateBy());
		lqw.eq(bo.getUpdateTime() != null, JobMarkRecord::getUpdateTime, bo.getUpdateTime());
		lqw.eq(StrUtil.isNotBlank(bo.getUpdateBy()), JobMarkRecord::getUpdateBy, bo.getUpdateBy());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(JobMarkRecordAddBo bo) {
		JobMarkRecord add = BeanUtil.toBean(bo, JobMarkRecord.class);
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(JobMarkRecordEditBo bo) {
		JobMarkRecord update = BeanUtil.toBean(bo, JobMarkRecord.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(JobMarkRecord entity) {
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}
}
